local skel = fk.CreateSkill {
  name = "emo__renzhu",
}

Fk:loadTranslationTable{
  ["emo__renzhu"] = "认主",
  [":emo__renzhu"] = "游戏开始时，你令一名其他角色获得“主”标记：每当该角色受到伤害后，你可以视为对伤害来源使用一张伤害基数+1的【杀】；当你受到伤害时，该角色可以防止此伤害，然后受到伤害来源的一点雷电伤害。",

  ["@@emo__master"] = "主",
  ["@emo__renzhu"] = "认主",
  ["#emo__renzhu-choose"] = "认主：选择一名其他角色作为你的“主”：其受到伤害后你可视为对来源使用【杀】",
  ["#emo__renzhu-slash"] = "认主：你可以视为对 %src 使用【杀】",
  ["#emo__renzhu-transfer"] = "认主：你可以防止 %src 即将受到的伤害，然后你受到1点雷电伤害",

  ["$emo__renzhu1"] = "我叫亚托莉！",
  ["$emo__renzhu2"] = "我是高性能的嘛",
}

skel:addEffect(fk.GameStart, {
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(skel.name) then
      return table.find(player.room.alive_players, function (p) return p ~= player end) ~= nil
      and player:getMark("emo__renzhu_tar") == 0
    end
  end,
  on_cost = function (self, event, target, player, data)
    local tos = player.room:askToChoosePlayers(player, { targets = player.room:getOtherPlayers(player, false),
     min_num = 1, max_num = 1, prompt = "#emo__renzhu-choose", skill_name = skel.name, cancelable = false })
    if #tos > 0 then
      event:setCostData(self, {tos = tos})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).tos[1]
    room:addPlayerMark(to, "@@emo__master")
    room:setPlayerMark(player, "emo__renzhu_tar", to.id)
    room:setPlayerMark(player, "@emo__renzhu", to.general)
  end,
})

skel:addEffect(fk.Damaged, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(skel.name) then
      if not target.dead and target.id == player:getMark("emo__renzhu_tar") and data.from and not data.from.dead then
        local slash = Fk:cloneCard("slash")
        slash.skillName = skel.name
        return not player:prohibitUse(slash) and not player:isProhibited(data.from, slash)
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    if player.room:askToSkillInvoke(player, { skill_name = skel.name, prompt = "#emo__renzhu-slash:"..data.from.id}) then
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local slash = Fk:cloneCard("slash")
    slash.skillName = skel.name
    player.room:useCard{ from = player, tos = {data.from}, card = slash, extraUse = true, additionalDamage = 1 }
  end,
})


skel:addEffect(fk.DamageInflicted, {
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(skel.name) then
      return target == player and player.room:getPlayerById(player:getMark("emo__renzhu_tar")) ~= nil
        and not player.room:getPlayerById(player:getMark("emo__renzhu_tar")).dead
    end
  end,
  on_cost = function (self, event, target, player, data)
    local master = player.room:getPlayerById(player:getMark("emo__renzhu_tar"))
    if player.room:askToSkillInvoke(master, { skill_name = skel.name, prompt = "#emo__renzhu-transfer:"..player.id }) then
      event:setCostData(self, {tos = {master}})
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    data:preventDamage()
    player.room:damage { from = data.from, to = player.room:getPlayerById(player:getMark("emo__renzhu_tar")), damage = 1,
      skillName = skel.name, damageType = fk.ThunderDamage }
  end
})

skel:addLoseEffect(function (self, player, is_death)
  local room = player.room
  if player:getMark("emo__renzhu_tar") ~= 0 then
    local master = room:getPlayerById(player:getMark("emo__renzhu_tar"))
    room:setPlayerMark(player, "emo__renzhu_tar", 0)
    room:setPlayerMark(player, "@emo__renzhu", 0)
    room:removePlayerMark(master, "@@emo__master")
  end
end)

-- 主人死亡后为自己清除标记
skel:addEffect(fk.Deathed, {
  can_refresh = function (self, event, target, player, data)
    return target.id == player:getMark("emo__renzhu_tar") and target.rest == 0
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:setPlayerMark(player, "emo__renzhu_tar", 0)
    player.room:setPlayerMark(player, "@emo__renzhu", 0)
  end,
})

return skel
